package devTest
{
	import devTest.Game.CEndGame;
	import devTest.Game.CGame;
	import devTest.Game.CPrincipal;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * ...
	 * @author miquel
	 */
	[Frame(factoryClass="devTest.Preloader")]
	public class Main extends Sprite 
	{
		private var timerEndMatch:		Timer 		= null;
		private var timerRestart:		Timer 		= null;
		private var endOfGame:			CEndGame 	= null;
		private var presentation:				CPrincipal 	= null;
		private var game:						CGame 		= null;
		
		
		
		/**
		 * constructor
		 */
		public function Main():void 
		{
			if (stage)
			{				
				init();
			}
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		/**
		 * init application
		 * @eventType : event
		 */
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
		
			//maintain the same ratio screen
			stage.scaleMode = StageScaleMode.EXACT_FIT;

			// entry point
			
			//set listeners to change screens in the correct moment
			//principal screen
			addEventListener( MouseEvent.CLICK, Start);
			presentation = new CPrincipal(stage);
			GoPrincipal();
	
			//to end game
			timerEndMatch = new Timer(500, 0);
			timerEndMatch.addEventListener(TimerEvent.TIMER, EndGame);
			
			//te restart game
			timerRestart = new Timer(5000, 1);
			timerRestart.addEventListener(TimerEvent.TIMER, RestartGame);
		}
		

		/**
		 * GoPrincipal: the first screen
		 */
		private function GoPrincipal():void
		{
			trace("4:Init Presentation");
			presentation.x = 0;
			presentation.y = 0;
			presentation.Init();
			addChild(presentation);
		}

		
		/**
		 * Start: when user push button go to game
		 * @eventType : MouseEv
		 */
		private function Start(e:MouseEvent):void
		{
			//to start game
			removeChild(presentation);
			game = new CGame(stage);
			GoGame();		
			timerEndMatch.start();
		}
		

		/**
		 * GoGame: Go to play game
		 */
		private function GoGame():void
		{
			trace("4:Init game");
			game.x = 0;
			game.y = 0;
			if (!game.Init())
			{
				trace("Main: init-> Problems initialing the game");
			}
			else
			{
				addChild(game);
			}
		}
		

		/**
		 * EndGame: detects when finishes the game
		 * @eventType : timerEv.
		 */
		private function EndGame(t:TimerEvent):void
		{
			if (game != null)
			{
				//State - 3 finished
				if (game.GetStateGame() == 3)
				{
					trace("Main: EndGame");
					timerEndMatch.stop();
					endOfGame = new CEndGame(stage);
					
					//shows the final result
					endOfGame.Init(game.GetGoalsA(), game.GetGoalsB());
					addChild(endOfGame);
					removeChild(game);
					
					//pauses any seconds
					timerRestart.start();
				}
			}
		}
		
		
		/**
		 * RestartGame: detects when the game restarts
		 * @eventType : timerEv.
		 */
		private function RestartGame(t:TimerEvent):void
		{
			timerRestart.stop();
			removeChild(endOfGame);
			presentation = new CPrincipal(stage);
			//goes to first screen
			GoPrincipal();
		}
	}

}